package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.RestaurantdisheslistBean;

import java.util.List;

import takeout.home.StoreinformationActivity;

public class RestaurantdisheslistApter extends RecyclerView.Adapter<RestaurantdisheslistApter.MyHolder>{
    private Context context;
    private List<RestaurantdisheslistBean.RowsBean> list;


    public RestaurantdisheslistApter(Context context, List<RestaurantdisheslistBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<RestaurantdisheslistBean.RowsBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RestaurantdisheslistApter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_restaurantdisheslist,null);
        return new RestaurantdisheslistApter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RestaurantdisheslistApter.MyHolder holder, int position) {

        try {
            if(!TextUtils.isEmpty(list.get(position).getImgUrl())){
                Glide.with(context).load(TokenBean.IP+list.get(position).getImgUrl()).into(holder.imgUrl);
            }

            if(!TextUtils.isEmpty(list.get(position).getName())){
                holder.name.setText(list.get(position).getName());
            }


            if(!TextUtils.isEmpty(String.valueOf(list.get(position).getScore()))){
                holder.score.setText("评分: "+String.valueOf(list.get(position).getScore()));
            }

            if(!TextUtils.isEmpty(String.valueOf(list.get(position).getAvgCost()))){
                holder.avgCost.setText("人均: "+String.valueOf(list.get(position).getAvgCost()));
            }

            if(!TextUtils.isEmpty(String.valueOf(list.get(position).getDeliveryTime()))){
                holder.deliveryTime.setText("配送时间: "+String.valueOf(list.get(position).getDeliveryTime()+"分钟"));
            }


            if(!TextUtils.isEmpty(String.valueOf(list.get(position).getDistance()))){
                holder.distance.setText("到店距离: "+String.valueOf(list.get(position).getDistance()+"m"));
            }

                holder.re.setLayoutManager(new GridLayoutManager(context,list.get(position).getProductList().size()));
                holder.re.setAdapter(new RestaurantdishesApter(context,list.get(position).getProductList()));

        } catch (Exception e) {
            e.printStackTrace();
        }
        //点击店家信息进入店家详情页
       /* holder.re.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, StoreinformationActivity.class);
                intent.putExtra(Commom.STOREINFORMATIONID,String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });*/
    }



    @Override
    public int getItemCount() {
        if(list.size()==0){
            return 0;
        }else {
            return list == null ? 0 :1;
        }

    }

    class MyHolder extends RecyclerView.ViewHolder {

        private final ImageView imgUrl;
        private final TextView name;
        private final TextView deliveryTime;
        private final TextView distance;
        private final TextView score;
        private final TextView avgCost;
        private final RecyclerView re;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgUrl = itemView.findViewById(R.id.imgUrl);
            name = itemView.findViewById(R.id.name);
            avgCost = itemView.findViewById(R.id.avgCost);
            score = itemView.findViewById(R.id.score);
            distance = itemView.findViewById(R.id.distance);
            deliveryTime = itemView.findViewById(R.id.deliveryTime);
            re = itemView.findViewById(R.id.re);
        }
    }


}







